local que_huanmeng = fk.CreateSkill {
    name = "que_huanmeng",
    tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
    ["que_huanmeng"] = "幻梦",
    ["@que_huanmeng"] = "",
    [":que_huanmeng"] = "锁定技，每轮开始时，或当你进入、离开濒死状态时，你可以记录一种未被记录的花色；其他角色的牌不因使用或打出进入弃牌堆后，若你已记录此牌的花色，你获得之。",
}
local spec = {
    anim_type = "offensive",
    on_use = function(self, event, target, player, data)
        local room = player.room
        local mark = player:getTableMark("@que_huanmeng")
        local chs = { "log_spade", "log_club", "log_heart", "log_diamond" }
        if table.contains(mark, "log_spade") then
            table.removeOne(chs, "log_spade")
        end
        if table.contains(mark, "log_club") then
            table.removeOne(chs, "log_club")
        end
        if table.contains(mark, "log_heart") then
            table.removeOne(chs, "log_heart")
        end
        if table.contains(mark, "log_diamond") then
            table.removeOne(chs, "log_diamond")
        end

        local chc = room:askToChoice(player, {
            choices = chs,
            skill_name = que_huanmeng.name,
        })
        room:addTableMarkIfNeed(player, "@que_huanmeng", chc)
    end,
}

que_huanmeng:addEffect(fk.RoundStart, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(que_huanmeng.name) and #player:getTableMark("@que_huanmeng") < 4
    end,
    on_use = spec.on_use,
})
que_huanmeng:addEffect(fk.EnterDying, {
    anim_type = "support",
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(que_huanmeng.name) and target == player and #player:getTableMark("@que_huanmeng") < 4
    end,
    on_use = spec.on_use,
})
que_huanmeng:addEffect(fk.AfterDying, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(que_huanmeng.name) and target == player and #player:getTableMark("@que_huanmeng") < 4
    end,
    on_use = spec.on_use,
})

que_huanmeng:addEffect(fk.AfterCardsMove, {
    can_trigger = function(self, event, target, player, data)
        local mark = player:getTableMark("@que_huanmeng")
        if player:hasSkill(que_huanmeng.name) then
            for _, move in ipairs(data) do
                if move.from ~= player and move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse then
                    for _, info in ipairs(move.moveInfo) do
                        local card = Fk:getCardById(info.cardId)
                        if table.contains(mark, card:getSuitString(true)) then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local mark = player:getTableMark("@que_huanmeng")
        for _, move in ipairs(data) do
            if move.from ~= player and move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse then
                for _, info in ipairs(move.moveInfo) do
                    local card = Fk:getCardById(info.cardId)
                    if table.contains(mark, card:getSuitString(true)) then
                        room:delay(500)
                        room:obtainCard(player, info.cardId, true, fk.ReasonPrey, player, que_huanmeng.name)
                    end
                end
            end
        end
    end,
})
que_huanmeng:addLoseEffect(function(self, player)
    player.room:setPlayerMark(player, "@que_huanmeng", 0)
end)
return que_huanmeng
